package com.gss.common.core.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gss.common.core.entity.PageQueryVo;
import com.gss.common.core.service.IBaseService;
import com.gss.entity.R;
import com.gss.entity.enums.ConstantEnum;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

/**
 * @author: DYang
 * @since: 2023/5/26
 * @description: 基础controller
 */
@Slf4j
public class BaseController<T> {

    @Autowired
    private IBaseService<T> baseService;

    @ApiOperation(value = "单个新增")
    @PostMapping("/insert")
    public R<String> insert(@RequestBody T entity) throws IllegalAccessException {
        if (entity == null) {
            return R.error(ConstantEnum.PARAM_ERROR);
        }
        boolean save = baseService.save(entity);
        if (save) {
            return R.ok(ConstantEnum.SUCCESS);
        }
        return R.error(ConstantEnum.INSERT_ERROR);
    }

    @ApiOperation(value = "批量新增")
    @PostMapping("/insertBatch")
    public R<String> insertBatch(@RequestBody List<T> entityList){
        if (CollUtil.isEmpty(entityList)) {
            return R.error(ConstantEnum.PARAM_ERROR);
        }
        boolean save = baseService.saveBatch(entityList);
        if (save) {
            return R.ok(ConstantEnum.SUCCESS);
        }
        return R.error(ConstantEnum.INSERT_ERROR);
    }

    @ApiOperation(value = "根据id单个修改")
    @PostMapping("/update")
    public R<String> update(@RequestBody T entity){
        if (entity == null) {
            return R.error(ConstantEnum.PARAM_ERROR);
        }
        boolean update = baseService.updateById(entity);
        if (update) {
            return R.ok(ConstantEnum.SUCCESS);
        }
        return R.error(ConstantEnum.UPDATE_ERROR);
    }

    @ApiOperation(value = "根据id批量修改")
    @PostMapping("/updateBatchById")
    public R<String> updateBatchById(@RequestBody List<T> entityList){
        if (CollUtil.isEmpty(entityList)) {
            return R.error(ConstantEnum.PARAM_ERROR);
        }
        boolean update = baseService.updateBatchById(entityList);
        if (update) {
            return R.ok(ConstantEnum.SUCCESS);
        }
        return R.error(ConstantEnum.UPDATE_ERROR);
    }

    @ApiOperation(value = "根据id单个删除")
    @DeleteMapping("/deleteById")
    public R<String> deleteById(Integer id){
        if (id == null) {
            return R.error(ConstantEnum.PARAM_ERROR);
        }
        boolean del = baseService.removeById(id);
        if (del) {
            return R.ok(ConstantEnum.SUCCESS);
        }
        return R.error(ConstantEnum.DEL_ERROR);
    }

    @ApiOperation(value = "根据id批量删除")
    @PostMapping("/deleteBatchByIds")
    public R<String> deleteBatchByIds(@RequestBody List<Integer> idList){
        if (idList == null || idList.size() == 0) {
            return R.error(ConstantEnum.PARAM_ERROR);
        }
        boolean del = baseService.removeBatchByIds(idList);
        if (del) {
            return R.ok(ConstantEnum.SUCCESS);
        }
        return R.error(ConstantEnum.DEL_ERROR);
    }

    @ApiOperation(value = "根据id查询")
    @GetMapping("/selectById")
    public R<T> selectById(Integer id){
        if (id == null) {
            return R.error(ConstantEnum.PARAM_ERROR);
        }
        T info = baseService.getById(id);
        return R.ok(info);
    }

    @ApiOperation(value = "根据id批量查询")
    @PostMapping("/selectBatchById")
    public R<List<T>> selectBatchById(@RequestBody List<Integer> idList){
        if (idList == null || idList.size() == 0) {
            return R.error(ConstantEnum.PARAM_ERROR);
        }
        List<T> list = baseService.listByIds(idList);
        return R.ok(list);
    }

    @ApiOperation(value = "根据条件分页查询")
    @PostMapping("/selectPage")
    public R<IPage<T>> selectPage(@RequestBody PageQueryVo<T> pageQueryVo){

        int pageNum = pageQueryVo.getPageNum() == null ? 1 : pageQueryVo.getPageNum();
        int pageSize = pageQueryVo.getPageSize() == null ? 10 : pageQueryVo.getPageSize();

        Page<T> page = new Page<>(pageNum,pageSize);
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("id");
        T param = pageQueryVo.getParam();
        if (param != null) {
            queryWrapper.setEntity(param);
        }

        IPage<T> pageInfo = baseService.page(page, queryWrapper);
        return R.ok(pageInfo);
    }


}
